<template>
	<om-formsub-outer :index="index" @save="save" @delete="del">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata:{},
				initValue:{
					bearUser3Id:'',
					bearUser3Name:'',
					evaUser3Id:'',
					evaUser3Name:''
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				bearOff3CacheKey:"obj-m-proj-bearoff3-cache",
				evaOff3CacheKey:'obj-m-proj-evaoff3-cache',
				linkParam:{}
			}
		},
		methods: {
			inputPlanTime(val){
				let _that  = this
				if(Number(val)%0.5 != 0){
					let num = Number(val)%0.5
					_that.$refs.autoform.formdata.planTime = Number(val) - Number(num) 
				}
			},
			clearBearOff(){
				let _that = this
				_that.$refs.autoform.clear("title2","bearOff3Id")
				uni.removeStorage({
					key:'searchUserDefaultList'
				})
			},
			setDefaultData(){
				let _that = this
				if(_that.formdata.bearOff3Id){
					let idList = _that.formdata.bearOff3Id.split(',')
					let nameList = _that.formdata.bearOff3Name.split(',')
					let data = []
					for(let index in idList){
						let idMixList = idList[index].split('/')
						let nameMixList = nameList[index].split('/')
						let obj = {
							id:idMixList[0],
							username:nameMixList[0],
							posId:idMixList[1],
							posName:nameMixList[1]
						}
						data.push(obj)
					}
					uni.setStorage({
						key:'searchUserDefaultList',
						data:data
					})
				}
			},
			initNum(){
				let _that = this
				_that.om.request(
					"dataDict/getIntByCode",
					{
						code:"DD021017003"
					}
				).then(res=>{
					_that.$refs.autoform.formdata.maxScore = Number(res.value)
				})
			},
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
					  key: this.cachename,
					  data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				if(this.index != null) {
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
					 key: this.cachename,
					 data: this.datas
				})
				uni.navigateBack()
			},
			onSelectBearOff3Id(userId){
				var editor = this.$refs.autoform.getEditor("title2","evaOff3Id");
				if(editor && editor.linkParam){
					editor.linkParam.other = JSON.stringify({
						noUserIdStr:userId
					})
				}
			},
			onSelectEvaOff3Id(userId){
				var editor = this.$refs.autoform.getEditor("title2","bearOff3Id");
				if(editor && editor.linkParam){
					editor.linkParam.other = JSON.stringify({
						noUserIdStr:userId
					})
				}
			},
			initConfig(e){
				let _that = this
				_that.initNum()
				changeEditor(_that.autoformdata[0].editors,"evaOff3Id","link","../../public/pageSearch/pageSearch")
				changeEditor(_that.autoformdata[0].editors, "evaOff3Id", "linkParam", {
					namefield:"userPosInstName",
					searchfield:"q",
					url:urlUtil.officePageUrl().url,
					cachename:_that.evaOff3CacheKey,
					placeholder:"请输入姓名或岗位"
				})
				
			}
		},
		onUnload(){
			uni.removeStorage({
				key:"searchUserDefaultList"
			})
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initConfig(e)
								_that.linkParam = {
											namefield: urlUtil.userPageUrl().namefield,
											searchfield: "q",
											cachename: _that.bearOff3CacheKey,
											isUserPos:true,
											placeholder: urlUtil.userPageUrl().prompt,
											pagination: true
										}
								changeEditor(_that.autoformdata[0].editors, "bearOff3Id", {
									'link': '../../public/searchUser/searchUser',
									"linkParam": _that.linkParam
								})
								_that.setDefaultData()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			_that.setDefaultData()
			if(_that.$refs.autoform){
				uni.getStorage({
					key: this.evaOff3CacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.evaOff3Id = d.id
							formdata.evaOff3Name = d.userName
							formdata.evaUser3Id = d.userId
							formdata.evaUser3Name = d.userName
							uni.removeStorage({
								key: _that.evaOff3CacheKey
							});
							_that.onSelectEvaOff3Id(d.userId)
						}
					}
				});
				uni.getStorage({
					key: this.bearOff3CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key:'searchUserDefaultList',
								data:d
							})
							var formdata = _that.$refs.autoform.formdata;
							if(!formdata.bearOff3Name){
								formdata.bearOff3Name = ''
							}
							let idStr = []
							let nameStr = []
							let user5NameList = []
							let bearOff3Name = ''
							if (formdata.bearOff3Name) {
								bearOff3Name = formdata.bearOff3Name.split(',')
							}
							for (let index in d) {
								idStr.push(d[index].id + '/'+d[index].posId)
								nameStr.push(d[index].username+'/'+d[index].posName)
								user5NameList.push(d[index].username)
							}
							let idList = idStr.map(e => e).join(",")
							let nameList = nameStr.map(e => e).join(",");
							let user5NameStr =  user5NameList.map(e => e).join(",")
							if (d.length) {
								if (idStr.length) {
									formdata.bearOff3Id = idList
									formdata.bearOff3Name = nameList
									formdata.bearUser3Name = user5NameStr
								}
							}
							uni.removeStorage({
								key: _that.bearOff3CacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
